#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mediaplayer.h"
#include "addradio.h"
#include "logger.h"



MainWindow::MainWindow(QWidget *parent)
   : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);



    QAction *quitAction = new QAction(tr("E&xit"), this);
    quitAction->setShortcut(tr("Ctrl+Q"));
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    addAction(quitAction);
    qLog.info("Setze close QAction auf Ctrl+Q");

    m_playlist = new XplReader();

    m_player = new MediaPlayer(this);
    ui->dial->setValue(m_player->getVolume());

    m_playlist->Open("radio.xpl");
    m_playlist->WriteInQComboBox(ui->comboBox);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton)
     {
         dragPosition = event->globalPos() - frameGeometry().topLeft();
         event->accept();
     }
 }
void MainWindow::mouseMoveEvent(QMouseEvent *event)
 {
     if (event->buttons() & Qt::LeftButton) {
         move(event->globalPos() - dragPosition);
         event->accept();
     }
 }
void MainWindow::timerEvent ( QTimerEvent * event )
{
    if(event->timerId() == m_timerID)
    {
        killTimer(m_timerID);


        if(ui->pushButton->text() == "*Play")
        {
            ui->pushButton->setText("Stop");
        }
        else if(ui->pushButton->text() == "*Stop")
        {
            ui->pushButton->setText("Play");
        }
    }
}

void MainWindow::on_pushButton_clicked()
{
    if(ui->pushButton->text() == "Play")
    {
        m_player->Play(m_playlist->getURL(ui->comboBox, m_curIndex));
        qLog.info("Start URL: " + m_playlist->getURL(ui->comboBox, m_curIndex));

        ui->dial->setValue(m_player->getVolume());
        qLog.info("Setze Player Lautstärke auf: " +  m_player->getVolume());

        ui->pushButton->setText("*Play");
        qLog.info("Setze PlayerButton Text auf *Play");
        ui->comboBox->setEnabled(false);

        m_timerID  = startTimer(1000);
        qLog.info("Starte PlayerButtonTextSwitch Timer");
    }
    else if(ui->pushButton->text() == "Stop")
    {
        m_player->Stop();
        qLog.info("Stop URL: " + m_playlist->getURL(ui->comboBox, m_curIndex));
        ui->pushButton->setText("*Stop");
        qLog.info("Setze PlayerButton Text auf *Stop");

        ui->comboBox->setEnabled(true);

        m_timerID  = startTimer(1000);
        qLog.info("Starte PlayerButtonTextSwitch Timer");
    }


}

void MainWindow::setInfo(QString info)
{
    ui->lblInfo->setText(info);
    qLog.info("Setze Info Text: " + info);
}

void MainWindow::on_dial_valueChanged(int value)
{
    m_player->setVolume(value);
    qLog.info("Setze Player Lautstärke auf " + value);
}

void MainWindow::on_comboBox_currentIndexChanged(int index)
{
    m_curIndex = index;
}

void MainWindow::on_cmdAdd_clicked()
{
    AddRadio *radio = new AddRadio(this);
    radio->show();
}
void MainWindow::AddEntry(xplEntry* entry)
{
    m_playlist->WriteInQComboBox(ui->comboBox, entry);
    m_playlist->SaveInFileFromQComboBox(ui->comboBox);
}

void MainWindow::on_MainWindow_destroyed()
{
    m_playlist->SaveInFileFromQComboBox(ui->comboBox);
}
